package com.dhy.zkconfig;

import com.dhy.msg.CommonResult;
import com.dhy.pojo.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Slf4j
@RestController
public class OrderController
{
    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource//负责完成远程接口调用，封装了httpClient
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        //POST 数据到一个URL，返回根据响应体匹配形成的对象
        //REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
        //注意: 这里请求参数是一个pojo.该参数会以json格式发送，即接收端需要使用@ResponseBody注解来接收json格式的请求参数
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id)
    {
       //POST 数据到一个URL，返回根据响应体匹配形成的对象
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
    }
}

